package com.nirvana.utils.security;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 
 * @author yshqiu
 */
public class MD5 {

	public static String md5(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();
		} catch (Exception e) {
			e.printStackTrace();

		}
		return str;
	}

	public static String md5String(String data) {
		MessageDigest digest = null;
		try {
			digest = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			return null;
		}
		digest.update(data.getBytes());
		byte hash[] = digest.digest();
		StringBuffer sb = new StringBuffer(hash.length * 2);
		for (int i = 0; i < hash.length; i++) {
			if (((int) hash[i] & 0xFF) < 0x10) {
				sb.append("0");
			}
			sb.append(Long.toString((int) hash[i] & 0xFF, 16));
		}
		return sb.toString().toLowerCase();
	}

	public static void main(String[] args) throws IOException {

		// System.out.println(DESUtils.getDecryptString("zHliZtZ/8AhYg51k6V3bag=="));
		// System.out.println(DESUtils.getEncryptString("Aa123456"));
		System.out.println(AESTools.decrypt("zHliZtZ/8AhYg51k6V3bag==", "123456789abcde56"));

	}
}
